package com.ysd.lis.service.inv.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ysd.common.Result;
import com.ysd.lis.entity.inv.InvCert;
import com.ysd.lis.mapper.inv.InvCertMapper;
import com.ysd.lis.request.Params;
import com.ysd.lis.request.inv.CertDto;
import com.ysd.lis.service.inv.InvCertService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wanghuaixing
 * @since 2024-09-10
 */
@Service
public class InvCertServiceImpl extends ServiceImpl<InvCertMapper, InvCert> implements InvCertService {

    @Autowired
    InvCertMapper invCertMapper;

    @Override
    public Result findCertList(Params params) {
        Page<InvCert> page = new Page<>();
        page.setCurrent(params.getPageIndex());
        page.setSize(params.getPageSize());
        LambdaQueryWrapper<InvCert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvCert::getDelFlag, 0);
        if (!StringUtils.isEmpty(params.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(InvCert::getCertCode, params.getSearchValue()).or().like(InvCert::getCertName, params.getSearchValue()));
        }
        Page<InvCert> invCertPage = invCertMapper.selectPage(page, queryWrapper);
        return Result.succ(1, "查询成功", invCertPage);
    }

    @Override
    public Result batchDeleteCert(CertDto vo) {
        List<InvCert> certList = vo.getCertList();

        List<String> ids = certList.stream().map(InvCert::getId).collect(Collectors.toList());

        boolean b = this.removeByIds(ids);
        if (b) {
            return Result.succ(1, "删除成功", null);
        } else {
            return Result.fail(400, "删除失败", "");
        }

    }

    @Override
    public Result addCert(InvCert invCert) {
        int i = invCertMapper.insert(invCert);
        if (i > 0) {
            return Result.succ(1, "新增成功", "");
        } else {
            return Result.fail("添加证书失败");
        }
    }

    @Override
    public Result updateCert(InvCert invCert) {
        int i = invCertMapper.updateById(invCert);
        if (i > 0) {
            return Result.succ(1, "修改成功", "");
        } else {
            return Result.fail("修改证书失败");
        }
    }

    @Override
    public Result findInvCertList(Params params) {
        LambdaQueryWrapper<InvCert> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(InvCert::getDelFlag, 0);
        if (!StringUtils.isEmpty(params.getSearchValue())) {
            queryWrapper.and(wrapper -> wrapper.like(InvCert::getCertCode, params.getSearchValue()).or().like(InvCert::getCertName, params.getSearchValue()));
        }
        List<InvCert> invCertPage = invCertMapper.selectList( queryWrapper);
        return Result.succ(1, "查询成功", invCertPage);
    }

}
